#include "SysHalt.h"

//休眠系统设计
/*
1.首次上电60秒内，BLE获取设备信息等初始化
2.休眠
3.由BLE唤醒引脚D2唤醒，2s一次，低电平6ms
4.当为APP唤醒，则为低电平6s
*/


volatile unsigned int gu16v_HaltTime;
unsigned char gu8v_sysBLE_TxDisconnect;

void fun_SysInit()
{
	WDTC_OFF(); //关闭看门狗 
	fun_GPIO_PowerOn();//PA口唤醒
	fun_RamInit();
	fun_ADC_Init();
	fun_UART_Init();
	fun_TimerInit();
}

void fun_SysHalt()
{
	if(gu8v_sysBLE_TxDisconnect == 0)
	{
		if(gu16v_HaltTime >= HALTTIME_MS / TIMER_MS)
		//if(gu16v_HaltTime >= 10000 / TIMER_MS)
		{
			BLE.flag.b.Tx_Disconnect = 1;
			gu8v_sysBLE_TxDisconnect = 1;	
		}
	}
	else if(gu8v_sysBLE_TxDisconnect == 1)
	{
		if(BLE.flag.b.Rx_Disconnect)
		{
			fun_TimerOff();
			fun_GPIO_PowerDown();
			_rie = 0;	//0：接收中断除能
			_emi = 0;
			GCC_HALT();
			GCC_DELAY(20000); //10ms
			if(BLE_WAKEUP_IO == 1) //为2s唤醒
			{
				gu16v_HaltTime = HALTTIME_MS / TIMER_MS;
			}
			else if(BLE_WAKEUP_IO == 0) //为APP唤醒,首次断开后，APP会重新唤醒两次
			{
				gu16v_HaltTime = 0; //重新计时休眠
				gu8v_sysBLE_TxDisconnect = 0;
				
				fun_GPIO_PowerOn();
				fun_TimerInit();
				fun_UART_Init();
				BLE.flag.b.Rx_Disconnect = 0;
			}
		}	
	}
}


